import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws IOException {
        String url = "http://49.235.95.23:9292/predict/ultra_light_fast_generic_face_detector_1mb_640";
        String openPath = "src\\main\\resources\\img\\gp.jpg";
//        String json = "{\"userName\":\"张三\",\"userAge\":\"22\",\"userSex\":\"男\"}";
//        JSONObject jsonObject = JsonUtils.getJsonObj("src\\main\\resources\\tmp\\tmp.json");
//        PostUtils.httpPostWithJSON(url, jsonObject.toJSONString());
        Map<String,Object> map = new HashMap<>();
        ArrayList<String> list = new ArrayList<>();
        list.add(Base64Utils.GetImageStr(openPath));
        map.put("images",list);
        //得到要post的jsonData
        String data = JSONObject.toJSON(map).toString();
        String resJson = PostUtils.httpPostWithJSON(url, data);
//        String resJson = JsonUtils.getJson("src\\main\\resources\\tmp\\test.json");
        // get array[{'data':[...]}]
        JSONArray array = JSONObject.parseObject(resJson).getJSONArray("results");
        // get {'data':[...]}
        String s = array.getString(0);
        // get [(坐标obj的列表)]
        JSONArray posObjList = JSONObject.parseObject(s).getJSONArray("data");
        //保存坐标 等等画图
        List<Map<String,Double>> posList = new ArrayList<>();
        for (int i = 0; i < posObjList.size(); i++) {
            //单个坐标
            String pos = posObjList.getString(i);
            JSONObject p = JSONObject.parseObject(pos);
            Double top = p.getDouble("top");
            Double left = p.getDouble("left");
            Double bottom = p.getDouble("bottom");
            Double right = p.getDouble("right");
            System.out.println("left: "+left+" right: "+right+" bottom: "+bottom+" top: "+top);
            HashMap<String, Double> tmp = new HashMap<>();
            tmp.put("top",top);
            tmp.put("left",left);
            tmp.put("bottom",bottom);
            tmp.put("right",right);
            posList.add(tmp);
        }

        //存图片
        ImgUtils.draw(openPath, "src\\main\\resources\\img\\res\\gp.jpg",posList);
    }
}
